// pages/detail.js
import Toast from 'tdesign-miniprogram/toast/index';

var StringUtil = require('../../utils/string.js');
const STORAGE_KEY_FAV = 'FAV-KEY';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //搜索字段
    runnerId: 0,
    name: '',

    //展示控制
    runner: {},
    isLoading: true,
    avatar: '',
    nationIcon: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var runner = JSON.parse(options.runner);
    let favRunnersData = wx.getStorageSync(STORAGE_KEY_FAV);
    var favRunners = favRunnersData ? JSON.parse(favRunnersData) : {};
    var isFav = favRunners && favRunners[runner.runnerId];
    var favStatus = isFav ? "yes" : "no";
    runner.favStatus = favStatus;
    this.setData({
      runnerId: options.runnerId?options.runnerId: runner.runnerId,
      runner: runner,
      name: StringUtil.toSUpper(runner.lastName + " " + runner.firstName),
      favStatus: favStatus, 
      avatar: "https://itra.run/" + runner.profilePic,
      nationIcon: "https://itra.run/" + runner.code,
      isLoading:true
    });
  
    wx.cloud.callFunction({
      name: "api_proxy",
      data: {
        url: "https://www.itra.run/api/Runner/Info/" + this.data.runnerId,
        method: 'GET',
        json: false,
        headers: {
          "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
        }
      },
      success: (res) => {
        runner.lastName = StringUtil.toSUpper(res.result.lastNameOriginal);
        runner.firstName = StringUtil.toSUpper(res.result.firstNameOriginal);
        runner.pi = res.result.pi;
        runner.profilePic = res.result.profilePicture ? "/Files/Photos/"+res.result.profilePicture : "/images/member-pics/default.jpg";
        runner.code = "/images/CountryFlags/"+res.result.code+".svg";
        if(favRunners[runner.runnerId]){
          favRunners[runner.runnerId] = runner;
          wx.setStorage({
            key: STORAGE_KEY_FAV,
            data: JSON.stringify(favRunners),
          });
        }
        this.setData({
          runner: runner,
          name: StringUtil.toSUpper(runner.lastName + " " + runner.firstName),
          avatar: "https://itra.run/" + runner.profilePic,
          nationIcon: "https://itra.run/" + runner.code,
        });
      },
      fail: (res) => {
        console.log("fail");
        console.log(res);
      }
    });
    

    wx.cloud.callFunction({
      name: "api_proxy",
      data: {
        url: "https://zhizhangcs.cn/races_new.php?id=" + runner.runnerId + "&name=" + runner.lastName + "." + runner.firstName,
        method: 'GET',
        json: false
      },
      success: (res) => {
        this.setData({
          resultList: res.result,
          isLoading: false,
        });
      },
      fail: (res) => {
        console.log(res);
        this.setData({
          isLoading: false
        });
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  addToFav(e) {
    var runner = e.currentTarget.dataset.runner;
    let favRunnersData = wx.getStorageSync(STORAGE_KEY_FAV);
    var favRunners = favRunnersData ? JSON.parse(favRunnersData) : {};
    var favStatus;
    if(favRunners[runner.runnerId]){
      delete favRunners[runner.runnerId];
      favStatus = "no";
      Toast({
        context: this,
        selector: '#t-toast',
        message: '已取消收藏',
        theme: 'warning',
        direction: 'column',
        duration: 800
      });
    }else{
      favRunners[runner.runnerId] = runner;
      favStatus = "yes";
      Toast({
        context: this,
        selector: '#t-toast',
        message: '已收藏',
        theme: 'success',
        direction: 'column',
        duration: 800
      });
    }
    wx.setStorage({
      key: STORAGE_KEY_FAV,
      data: JSON.stringify(favRunners),
    });
    favRunnersData = wx.getStorageSync(STORAGE_KEY_FAV);
    this.setData({
      favStatus:favStatus
    });
  },
})